package db.freebase;

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.FileReader;
import java.io.IOException;

public class FreeBaseTSVReader implements Closeable {

    private static final String VALUE_SEPARATOR = "\t";
    private FreeBaseTSVHeader header;
    private BufferedReader reader;
    
    
    public FreeBaseTSVReader(String filename) throws IOException {
        reader = new BufferedReader(new FileReader(filename));
        String[] headerColumns = reader.readLine().split(VALUE_SEPARATOR);
        header = new FreeBaseTSVHeader(headerColumns);
    }

    public void close() throws IOException {
        if (reader != null) {
            reader.close();
        }
    }
    
    public FreeBaseTSVLine readLine() throws IOException {
        String rawLine = reader.readLine();
        return rawLine == null ? null : new FreeBaseTSVLine(rawLine.split(VALUE_SEPARATOR));
    }

    public FreeBaseTSVHeader getHeader() {
        return header;
    }
    
}
